package com.cheng.testnetwork;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.cheng.testnetwork.network.DownInfo;
import com.cheng.testnetwork.network.HttpDownManager;
import com.cheng.testnetwork.network.HttpSyncDownManager;
import com.cheng.testnetwork.util.LogUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * SD卡路径
     */
    public final static String SD_FILE = Environment.getExternalStorageDirectory().getPath() + "/AAAA";

    private Button btnDownLittle, btnDownBig,btnDownStop;

    ExecutorService poolExecutor = new ThreadPoolExecutor(3,64,
            300, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

    }

    private void initView() {

        btnDownLittle = findViewById(R.id.btn_download_little);
        btnDownBig = findViewById(R.id.btn_download_big);
        btnDownStop = findViewById(R.id.btn_download_stop);

        btnDownLittle.setOnClickListener(this);
        btnDownBig.setOnClickListener(this);
        btnDownStop.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_download_little:
                downloadLittle();
                break;
            case R.id.btn_download_big:
                downloadBig();

                break;
            case R.id.btn_download_stop:
                HttpSyncDownManager.getInstance().stopDown();

                break;
            default:
                break;
        }
    }


    private void downloadBig() {
        poolExecutor.execute(() ->{
            DownInfo info = new DownInfo();
            info.setUrl("http://static.zhengbajing.net/qldyx06.flv");
            info.setSavePath(SD_FILE+"/qldyx06.flv");
            HttpSyncDownManager.getInstance().startDown(info);
        });
//        DownInfo info = new DownInfo();
//        info.setUrl("http://static.zhengbajing.net/qldyx06.flv");
//        info.setSavePath(SD_FILE+"/qldyx06.flv");
//        info.setListener(new HttpProgressOnNextListener<DownInfo>() {
//            @Override
//            public void onNext(DownInfo baseDownEntity) {
//                LogUtil.i( "下载下一个视频： url=" + baseDownEntity.getUrl());
//            }
//
//            @Override
//            public void onStart() {
//                LogUtil.i("开始下载视频");
//            }
//
//            @Override
//            public void onComplete(DownInfo downInfo) {
//                LogUtil.i("下载视频--》 视频下载完成： url=" + downInfo.toString());
//            }
//
//            @Override
//            public void updateProgress(long readLength, long countLength) {
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                super.onError(e);
//                LogUtil.e(e != null ? e.getMessage() : "视频下载错误");  //后台报404
//            }
//
//            @Override
//            public void onPuase() {
//                super.onPuase();
//                LogUtil.i("onPuase（） ");
//            }
//
//            @Override
//            public void onStop() {
//                super.onStop();
//                LogUtil.i( "onStop（） ");
//            }
//
//        });
//        HttpDownManager.getInstance().startDown(info);

    }

    private void downloadLittle() {
//        poolExecutor.execute(() -> {
//            DownInfo info = new DownInfo();
//            info.setUrl("http://ad.zhengbajing.net/Uploads/video/59ce112c0aa697koqhw.mp4");
//            info.setSavePath(SD_FILE +"/59ce112c0aa697koqhw.mp4");
//            HttpSyncDownManager.getInstance().startDown(info);
//        });

        DownInfo info = new DownInfo();
        info.setUrl("http://ad.zhengbajing.net/Uploads/video/59ce112c0aa697koqhw.mp4");
        info.setSavePath(SD_FILE +"/59ce112c0aa697koqhw.mp4");
        info.setListener(new HttpProgressOnNextListener<DownInfo>() {
            @Override
            public void onNext(DownInfo baseDownEntity) {
                LogUtil.i( "下载下一个视频： url=" + baseDownEntity.getUrl());
            }

            @Override
            public void onStart() {
                LogUtil.i("开始下载视频");
            }

            @Override
            public void onComplete(DownInfo downInfo) {
                LogUtil.i("下载视频--》 视频下载完成： url=" + downInfo.toString());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                LogUtil.e(e != null ? e.getMessage() : "视频下载错误");  //后台报404
            }

            @Override
            public void onPuase() {
                super.onPuase();
                LogUtil.i("onPuase（） ");
            }

            @Override
            public void onStop() {
                super.onStop();
                LogUtil.i( "onStop（） ");
            }

            @Override
            public void updateProgress(long readLength, long countLength) {
            }
        });
        HttpDownManager.getInstance().startDown(info);

    }


}
